package top.chukongxiang.project.base.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import top.chukongxiang.project.base.mapper.user.UserMapper;
import top.chukongxiang.project.base.model.entity.sys.Menu;
import top.chukongxiang.project.base.model.entity.user.User;
import top.chukongxiang.project.base.model.vo.KeywordsPageVo;
import top.chukongxiang.project.base.model.vo.user.UserVo;
import top.chukongxiang.project.base.service.KeywordsPageService;
import top.chukongxiang.project.base.service.user.UserService;

import java.util.List;

/**
 * 用户服务实现
 * @author 楚孔响
 * @version 1.0
 * @date 2021/12/11 0:54
 */
@Service
public class UserServiceImpl extends KeywordsPageService<UserMapper, User> implements UserService {

    /**
     * 根据用户名获取用户
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public User getUserInfoByUsername(String username) {
        return baseMapper.selectInfoByUsername(username);
    }

    @Override
    public boolean existsById(String id) {
        return count(Wrappers.<User>lambdaQuery().eq(User::getId, id)) > 0;
    }

    @Override
    public List<Menu> listMenuByUserId(String userId) {
        return baseMapper.listMenuByUserId(userId);
    }

    @Override
    public IPage<UserVo> pageUser(KeywordsPageVo<User> keywordsPageVo) {
        QueryWrapper<User> wrapper = Wrappers.<User>query()
                .and(!StringUtils.isEmpty(keywordsPageVo.getKeywords()), userQueryWrapper -> userQueryWrapper
                        .like("u.user_name", keywordsPageVo.getKeywords())
                        .or()
                        .like("r.role_name", keywordsPageVo.getKeywords()))
                ;
        return baseMapper.pageUser(keywordsPageVo, wrapper);
    }

    @Override
    public IPage<? extends User> pageUserByRoleId(KeywordsPageVo<User> keywordsPageVo) {
        if (keywordsPageVo.getRoleId() == null) {
            return keywordsPageVo;
        }
        return baseMapper.pageUserByRoleId(keywordsPageVo, Wrappers.<User>query()
                .eq("ru.role_id", keywordsPageVo.getRoleId()));
    }
}
